<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Touch
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Core.php 2011-04-26 11:18:13 mirlan $
 * @author     Mirlan
 */

/**
 * @category   Application_Extensions
 * @package    Touch
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Touch_Plugin_Core extends Zend_Controller_Plugin_Abstract {
	public function routeStartup(Zend_Controller_Request_Abstract $request) {
		
		//Redirect upload action'
		

		$uploadAction = $request->getParam ( 'touch-upload-action', null );
		
		if (Engine_Api::_ ()->touch ()->isTouchMode () && $uploadAction !== null) {
			$request->setRequestUri ( $uploadAction );
		}
	}
	
	public function routeShutdown(Zend_Controller_Request_Abstract $request) {
		if (Engine_Api::_ ()->touch ()->siteMode () !== 'touch') {
			return;
		}
		
		$module = $request->getModuleName ();
		$controller = $request->getControllerName ();
		$action = $request->getActionName ();
		
		if ($module == 'hecore' && $controller == 'module' && $action == 'license') {
			return;
		}
		
		if (preg_match ( '/^admin-/', $controller )) {
			return;
		}
		
		// Social DNA
		if ($module == 'socialdna') {
			$module = 'user';
			$request->setModuleName ( $module );
		}
		
		// Mode Switch
		if (($module == 'mobile' && $controller == 'index' && $action == 'mode-switch') || ($module == 'touch' && $controller == 'index' && $action == 'touch-mode-switch')) {
			return;
		}
		
		// DashBoard
		if ($module == 'mobile' && $controller == 'index' && $action == 'index') {
			$request->setModuleName ( 'touch' );
		}
		
		if ($module != 'touch' && $module != 'mobile') {
			
			if (false === Engine_Api::_ ()->touch ()->redirectController ( $module )) {
				$request->setModuleName ( 'touch' );
				$request->setControllerName ( 'error' );
				$request->setActionName ( 'notfound' );
			}
		}
		
		if ($request->getParam ( 'format' ) == 'smoothbox') {
			$request->setParam ( 'format', 'html' );
		}
		
		Engine_Api::_ ()->touch ()->setLayout ();
	}
	
	public function preDispatch(Zend_Controller_Request_Abstract $request) {
		if (Engine_Api::_ ()->touch ()->siteMode () !== 'touch') {
			return;
		}
		// Social DNA
		if ($request->getModuleName () == 'socialdna') {
			$request->setModuleName ( 'user' );
		}
		
		if ($request->getControllerName () == 'error' && ($request->getModuleName () == 'core' || $request->getModuleName () == 'mobile')) {
			$request->setModuleName ( 'touch' );
		}
	}
}